package com.imut.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.imut.oa.mapper.*;
import com.imut.oa.pojo.entity.Expense;
import com.imut.oa.pojo.entity.Leave;
import com.imut.oa.pojo.entity.Task;
import com.imut.oa.pojo.entity.User;
import com.imut.oa.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private ExpenseMapper expenseMapper;

    @Autowired
    private LeaveMapper leaveMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DepartmentMapper departmentsMapper;
    @Autowired
    private AttendanceMapper attendanceMapper;
    @Override
    public Map<String, Long> getAllTrendData() {
        Map<String, Long> result = new HashMap<>();

        // 查询报销待处理数量
        Long expensePendingCount = expenseMapper.selectCount(new LambdaQueryWrapper<Expense>().eq(Expense::getStatus, 0));
        result.put("expensePendingCount", expensePendingCount);

        // 查询请假待处理数量
        Long leavePendingCount = leaveMapper.selectCount(new LambdaQueryWrapper<Leave>().eq(Leave::getStatus, 0));
        result.put("leavePendingCount", leavePendingCount);

        // 查询进行中任务数量
        Long ongoingTaskCount = taskMapper.selectCount(new LambdaQueryWrapper<Task>().eq(Task::getStatus, 1));
        result.put("ongoingTaskCount", ongoingTaskCount);

        // 查询员工数量
        Long employeeCount = userMapper.selectCount(new LambdaQueryWrapper<User>()
                .eq(User::getUserStatus, 0)
                .eq(User::getIsDelete, 0));
        result.put("employeeCount", employeeCount);

        return result;
    }
    @Override
    public List<Map<String, Object>> getDepartmentStats() {
        // 调用 DepartmentsMapper 中的方法获取部门人员分布数据
        return departmentsMapper.getDepartmentStats();
    }

    // HomeServiceImpl.java
    @Override
    public Map<String, Object> getAttendanceStats() {
        // 获取数据库中的原始统计数据
        List<Map<String, Object>> rawData = attendanceMapper.getAttendanceStats();

        // 转换为前端需要的格式
        List<String> months = new ArrayList<>();
        List<Integer> values = new ArrayList<>();

        for (Map<String, Object> item : rawData) {
            months.add((String) item.get("month"));
            values.add(Integer.parseInt(item.get("value").toString()));
        }

        Map<String, Object> result = new HashMap<>();
        result.put("months", months);
        result.put("values", values);

        return result;
    }
}